#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QObject>

#include <QTcpSocket>
#include <QHostAddress>

class TcpClient : public QObject
{
    Q_OBJECT
public:
    TcpClient(QObject *parent = nullptr);
    ~TcpClient();

    void connectToServer(const QString &hostAddress, quint16 port);
    void sendData(const QByteArray &data);

signals:
    void connectedToServer();
    void disconnectedFromServer();
    void dataReceived(const QByteArray &data);
    void errorOccurred(QAbstractSocket::SocketError error);

private slots:
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError error);

private:
    std::unique_ptr<QTcpSocket> socket; // 使用智能指针管理QTcpSocket
};

#endif // TCPCLIENT_H
